package org.popupcommunication.dao.impl;

import java.util.List;

import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.popupcommunication.dao.GroupDAO;
import org.popupcommunication.model.Group;
import org.popupcommunication.model.SujetForum;
import org.springframework.stereotype.Repository;


@Repository
public class GroupJpa extends GenericRepositoryJpa<Group> implements GroupDAO{

	@Override
	public Group findByName(String name) {
		TypedQuery<Group> query =  this.getEntityManager().createQuery("SELECT g FROM Group g", Group.class);
		List<Group> results = query.getResultList();					
		Group gege=null;
		for(Group gr:results){
			if (gr.getName().equals(name) ){
			   gege = gr;
			}   
		}		
		return gege;
	}

	@Override
	public Group findById(int id) {
		TypedQuery<Group> query =  this.getEntityManager().createQuery("SELECT g FROM Group g", Group.class);
		List<Group> results = query.getResultList();					
		Group gege=null;
		for(Group gr:results){
			if (gr.getIdGroup() == id ){
			   gege = gr;
			}   
		}		
		return gege;
	}
	@Override	
	public Group findBySlug(String slug){
		Query query = this.getEntityManager().createQuery(
				"SELECT g FROM Group g WHERE g.slug=:slug");
		query.setParameter("slug", slug);
		Group g = (Group) query.getSingleResult();
		return g;
	}
	
	@Override
	public List<Group> getAllGroupDao() {
		TypedQuery<Group> query =  this.getEntityManager().createQuery("SELECT g FROM Group g", Group.class);
		List<Group> results = query.getResultList();
		return results;
	}

}
